package pri.zxx.excelgenerate.dataentity.factory;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import pri.zxx.excelgenerate.dataentity.entity.CreatRequest;
import pri.zxx.excelgenerate.dataentity.entity.ModuleInfoEnum;
import pri.zxx.excelgenerate.dataentity.entity.ModuleMetaInfo;
import pri.zxx.excelgenerate.dataentity.module.ModuleCreator;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 妊娠母猪 模块创建工厂
 */
@Component
@Slf4j
public class ModuleFactory implements IAnimalModuleFactory {


    private final List<ModuleCreator> moduleCreators;

    public ModuleFactory(List<ModuleCreator> moduleCreators) {
        this.moduleCreators = moduleCreators;
    }

    @Override
    public List<ModuleMetaInfo> createModules(CreatRequest creatRequest) {
        List<ModuleInfoEnum> moduleInfoEnums = creatRequest.getCreateModuleInfoEnums();

        /****************************     筛选需要创建的module     **************************/
        List<ModuleCreator> collect = moduleCreators.stream()
                .filter(moduleCreator -> moduleInfoEnums.contains(moduleCreator.moduleInfo())).collect(Collectors.toList());

        //创建module
        List<ModuleMetaInfo> modules = collect.stream().map(moduleCreator -> moduleCreator.creator(creatRequest))
                .filter(Objects::nonNull)
                .flatMap(Collection::stream).collect(Collectors.toList());

        String moduleListName = modules.stream().map(ModuleMetaInfo::getModuleInfoEnum)
                .map(ModuleInfoEnum::getName).collect(Collectors.joining("、"));

        log.info("GestatingSowsModuleFactory 创建的模块：{}", moduleListName);
        return modules;
    }
}
